/*
 * Reverse digits of an integer.
 * Example1: x = 123, return 321
 * Example2: x = -123, return -321
 */
#include <limits.h>

class Solution {
public:
    int reverse(int x) {
        bool neg = false;
        int res = 0;
        if (x < 0) {
            if (x == INT_MIN) {
                return 0;
            }
            neg = true;
            x = -x;
        }
        while (x) {
            if (res > INT_MAX/10) {
                return 0;
            }
            res *= 10;
            res += x%10;
            x /= 10;
        }
        if (neg) {
            if (res == INT_MIN) {
                return 0;
            }
            return -res;
        }
        return res;
    }
};

int main() {
    return 0;
}
